#include <QtGui>
#include <QtNetwork>
#include "fenetre.h"
#include "horaire.h"
#include <QVBoxLayout>
#include <QTimer>
#include <QMenu>
#include <QAction>
#include <QIcon>
#include <QSettings>
#include <QList>


/*
  Ici tout à <<l'air>> de fonctionner correctement hormis un bug au niveau de l'affichage, sur plusieurs arrêt un seul prit en compte

  Peut etre un <peu> le bordel

  TODO:

  _faire le ménage
  _des coms
  _clean les fonctions avec params superflues

  */


fenetre::fenetre(QWidget *parent)

{
    //STYLE
    //setStyleSheet("background:transparent;");
    //setAttribute(Qt::WA_TranslucentBackground);
    //setWindowFlags(Qt::FramelessWindowHint);
    //Pref_Window

    QSettings settings("TramNow","general");
    restoreGeometry(settings.value("pos").toByteArray());

    //DEBUT SYSTRAY
    tray = new QSystemTrayIcon(this);
    QMenu* context_menu = new QMenu(this);
    QAction *afficher_action = new QAction("Afficher/Masquer",this);
    QAction *preference_action = new QAction("Preference",this);
    preference_action->setIcon(QIcon(":icon/conf.png") );
    QAction *quitter_action = new QAction("Quitter",this);
    quitter_action->setIcon(QIcon(":icon/quit.png"));
    QAction *deplacer_action = new QAction("Deplacer",this);
    QIcon icon(":/icon/systray.png");



    //context_menu->addAction(afficher_action);
    context_menu->addAction(preference_action);
    //context_menu->addAction(deplacer_action);
    context_menu->addAction(quitter_action);

    tray->setContextMenu(context_menu);
    tray->setIcon(icon);
    tray->show();

    connect(quitter_action,SIGNAL(triggered()),this,SLOT(quitter()));
    connect(preference_action,SIGNAL(triggered()),this,SLOT(show_pref()));
    connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(Notification(QSystemTrayIcon::ActivationReason)));



    //FIN SYSTRAY


   if (ChargerConf_Objets() != 1)
   {
        show_pref();

   };
    //evenement fenetre

}


void fenetre::show_pref()
{
   pref_win = new Pref_Window;
   pref_win->show();
      // connect(pref_win,SIGNAL(accepted()),this,SLOT(chargerAfficherConfig()));
}

void fenetre::quitter()
{
   // QApplication::quitOnLastWindowClosed();
   // QApplication::closeAllWindows();
    this->close();
}


void fenetre::writeSettings()
{
    qDebug()<< "Sauvegarde des parametres";
    QSettings settings("TramNow","general");
    settings.setValue("pos",saveGeometry());
/*
    settings.remove("pos");
    settings.setValue("pos",pos());
    qDebug()<< "saved value"<<os();
    //settings.sync();*/

}

void fenetre::readSettings()
{
//rien
}


//Evenement de fermeture

void fenetre::closeEvent(QCloseEvent *event)
{
writeSettings();
}

int fenetre::ChargerConf_Objets()
{

    //TIMER
    QTimer *timer = new QTimer(this);
    timer->start(1000 * 30);

    //Chargement des paramètres

    //fin chargement params

        //creation des listes d'objets
        //Création d'un tableau d'horaire
       QList<QLabel*>  l_label;   //Création d'un tableau QLabel
       QStringList list_code_arret;

       QSettings save("TramNow","arrets");
       QStringList cle = save.allKeys();
       if (cle.count() != 0 )
       {

           for (int i = 0 ; i <= cle.count() -1  ; i++)
           {

               l_horaire << new horaire(save.value(cle.at(i)).toString(),this);
               connect(l_horaire.at(i),SIGNAL(Refresh_as_texte_by_id_lstring(int,QString)),this,SLOT(set_lstring_by_id(int,QString)));

           }
          return 1;
       }else
       {
       return 0;
       }

}

void fenetre::Notification(QSystemTrayIcon::ActivationReason why_activated)

{

    if (why_activated == QSystemTrayIcon::Trigger && !is_refreshing)
    {
        //Clean
        all_horaire.clear();
        nbr_passage = 0;

        tray->setIcon(QIcon(":/icon/refresh.png"));
        //chaque Objets se rafraichi
        for (int i= 0 ; i <= l_horaire.count() -1 ;i++)
        {
           l_horaire.at(i)->refresh_as_text(i);
                      //qDebug()<< l_string.at(i);
        }


 //       tray->showMessage("Horaires","Les horaires",QSystemTrayIcon::Critical);
    }
}

void fenetre::TextToNotif(QString texte)
{



}

void fenetre::set_lstring_by_id(int i,QString texte)
{
    qDebug()<< QString::number(nbr_passage)<< texte;
    all_horaire.append(texte);
    nbr_passage ++;


    if (nbr_passage > l_horaire.count() -1 )
    {
    tray->showMessage("Mes horaires de bus",all_horaire,QSystemTrayIcon::Information);
    all_horaire.clear();
    tray->setIcon(QIcon(":icon/systray.png"));
    is_refreshing = false;
    }
}


